package com.ruoyi.web.service.warning.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.StateConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.domain.warning.WarningAddressBook;
import com.ruoyi.web.domain.warning.WarningConfig;
import com.ruoyi.web.mapper.warning.WarningAddressBookMapper;
import com.ruoyi.web.pojo.request.warning.WarningAddressBookRequest;
import com.ruoyi.web.service.warning.WarningAddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 五情预警通讯录 服务实现类
 * @author fwx
 * @date 2024-01-31 14:08:37
 */
@Service
public class WarningAddressBookServiceImpl extends ServiceImpl<WarningAddressBookMapper, WarningAddressBook> implements WarningAddressBookService {

    @Autowired
    private TokenService tokenService;


    /**
    * 新增五情预警通讯录
    */
    @Override
    public void insert(WarningAddressBook book){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        if(loginUser == null){
            throw new RuntimeException("用户未登录");
        }

        book.setUserId(loginUser.getUserId());

        book.setIsEnable(StateConstants.ENABLE);
        book.setCrtTime(LocalDateTime.now());

        super.save(book);
    }

    /**
    * 修改五情预警通讯录
    */
    @Override
    public void update(WarningAddressBook WarningAddressBook){
        super.updateById(WarningAddressBook);
    }

    @Override
    public void delete(String ids){
        if(StringUtils.isBlank(ids)){
            throw new RuntimeException("通讯录id不能为空");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        super.removeBatchByIds(idList);
    }

    @Override
    public void addImport(List<WarningAddressBook> books){

        if(books.size() == 0){
            return;
        }

        //查询所有的手机号
        List<WarningAddressBook> all = this.queryAll();

        Map<String, WarningAddressBook> allMap =
                all.stream().collect(Collectors.toMap(WarningAddressBook::getContactPhone, x -> x));

        //过滤手机号重复的
        for (WarningAddressBook book : books) {
            if (allMap.get(book.getContactPhone()) == null){
                this.insert(book);
            }
        }
    }

    /**
    *  分页查询五情预警通讯录
    */
    @Override
    public void queryPage(IPage<WarningAddressBook> page){
        QueryWrapper<WarningAddressBook> query  = new QueryWrapper<>();
        super.page(page,query);
    }

    @Override
    public List<WarningAddressBook> queryPage(WarningAddressBookRequest request){
        QueryWrapper<WarningAddressBook> query  = new QueryWrapper<>();
        request.bulidWrapper(query);
        List<WarningAddressBook> list = super.list(query);
        return list;
    }

    @Override
    public void updateOrSaveBook(WarningAddressBook book){

        if(StringUtils.isBlank(book.getContactPhone()) || StringUtils.isBlank(book.getContactEmail())){
            throw  new RuntimeException("手机号和邮箱不能为空");
        }

        WarningAddressBook byPhone = findByPhone(book.getContactPhone());

        if(book.getId() != null){

            if(byPhone != null && Objects.equals(byPhone.getContactPhone(),book.getContactPhone())  && !Objects.equals(byPhone.getId(),book.getId())){
                throw  new RuntimeException("手机号已经添加");
            }

            this.update(book);
            return;
        }

        if(byPhone != null && Objects.equals(byPhone.getContactPhone(),book.getContactPhone())){
            throw  new RuntimeException("手机号已经添加");
        }


        this.insert(book);
    }

    /**
    * 查询所有五情预警通讯录
    */
    @Override
    public List<WarningAddressBook> queryAll(){
        QueryWrapper<WarningAddressBook> query  = new QueryWrapper<>();
        List<WarningAddressBook> list = super.list(query);
        return list;
    }

    /**
    * 根据id查询五情预警通讯录
    */
    @Override
    public WarningAddressBook findById(String id){
        WarningAddressBook res = super.getById(id);
        return res;
    }

    @Override
    public WarningAddressBook findByPhone(String phone){
        QueryWrapper<WarningAddressBook> query  = new QueryWrapper<>();
        query.lambda().eq(WarningAddressBook::getContactPhone,phone);
        WarningAddressBook res = super.getOne(query,false);
        return res;
    }

}
